package com.ga.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ga.model.Album;
import com.ga.model.Reply;
import com.ga.model.User;
import com.ga.model.Video;
import com.ga.util.Common;
import com.ga.util.MessageUtil;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class ReplyController extends Controller {
	@ActionKey("/reply/list")
	public void list(){
		try {
			Map<String,Object> paraMap=new HashMap<String, Object>();
			paraMap.put("u.uname",getPara("uname"));
			paraMap.put("v.title",getPara("title"));
			paraMap.put("r.content",getPara("content"));
			paraMap.put("r.status=",getPara("status"));
			paraMap.put("r.isenable=",1);
			paraMap.put("r.createtime1",getPara("createtime1"));
			paraMap.put("r.createtime2",getPara("createtime2"));
			Map<String,Object> wheresMap=Common.wheres(paraMap);
			Object[] param=(Object[])wheresMap.get("param");
			int pn = Integer.parseInt(getPara("pageNumber")==null?"1":getPara("pageNumber"));
			int ps = Integer.parseInt(getPara("pageSize")==null?"10":getPara("pageSize"));	
			String select="select u.uname,v.title,r.id,r.parentid,r.content,r.createtime,"+Common.replycase();
			String sqlExceptSelect="from reply r left join users u on(r.users_id=u.id) left join video v on(r.video_id=v.id) where ";
			Page<Reply> list=Reply.dao.paginate(pn, ps, select, sqlExceptSelect+wheresMap.get("wheres"),param);
			renderJson(list);
		} catch (Exception e) {
			renderJson("flag",MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
	}
	@ActionKey("/reply/save")
	public void add(){
		try {
			Reply reply=new Reply();
			reply.set("content", getPara("content"));
			reply.set("users_id", getPara("users_id"));
			reply.set("video_id", getPara("video_id"));
			reply.set("parentid", getPara("parentid"));
			reply.save();
			renderJson("flag",MessageUtil.SUCCESS.toString());
		} catch (Exception e) {
			renderJson("flag",MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
	}
	@ActionKey("/reply/pass")
	public void pass(){
		Object[] param={getPara("status"),getPara("id")};
		try {
			Db.update("update reply set status = ? where id = ?",param);
			renderJson("flag",MessageUtil.SUCCESS.toString());
		} catch (Exception e) {
			renderJson("flag",MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
	}
	@ActionKey("/reply/detail")
	public void detail(){
		Map<String,Object> map=new HashMap<String,Object>();
		try {
			String id=getPara(0)==null?getPara("id"):getPara(0);
			String select="select u.uname,v.title,r.id,r.parentid,r.content,r.createtime,"+Common.replycase();
			String sqlExceptSelect=" from reply r left join users u on(r.users_id=u.id) left join video v on(r.video_id=v.id) where r.id=?";
			String sql=select+sqlExceptSelect;
			Reply reply=Reply.dao.findFirst(sql,id);
			map.put("flag", MessageUtil.SUCCESS.toString());
			map.put("reply", reply);
		} catch (Exception e) {
			map.put("flag", MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
		renderJson(map);
	}
	@ActionKey("/reply/delete")
	public void delete(){
		String id=getPara(0)==null?getPara("id"):getPara(0);
		try {
			Db.update("update reply set isenable = 0 where id = ?",id);
			Db.update("update reply set isenable = 0 where parentid = ?",id);
			List<Reply> clist=Reply.dao.find("select id from reply where parentid = ? ",id);
			delchild(clist);
			renderJson("flag",MessageUtil.SUCCESS.toString());
		} catch (Exception e) {
			renderJson("flag",MessageUtil.FAILED.toString());
			e.printStackTrace();
		}
	}
	private void delchild(List<Reply> list){
		for (int i = 0; i < list.size(); i++) {
			String id=list.get(i).get("id").toString();
			List<Reply> clist=Reply.dao.find("select id from reply where parentid = ? ",id);
			if(clist.size()>0){
				Db.update("update reply set isenable = 0 where parentid = ?",id);
				delchild(clist);
			}
		}
	}
}
